"use strict";

require("core-js/modules/es.object.define-property.js");

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.GenerateMd = void 0;

require("core-js/modules/es.array.concat.js");

require("core-js/modules/es.array.iterator.js");

require("core-js/modules/es.map.js");

require("core-js/modules/es.object.to-string.js");

require("core-js/modules/es.string.iterator.js");

require("core-js/modules/web.dom-collections.iterator.js");

require("core-js/modules/es.array.for-each.js");

require("core-js/modules/web.dom-collections.for-each.js");

require("core-js/modules/es.function.name.js");

var _posix = require("path/posix");

var _dir = require("../fs/dir");

var _generateClass = require("./generateClass");

function _slicedToArray(arr, i) { return _arrayWithHoles(arr) || _iterableToArrayLimit(arr, i) || _unsupportedIterableToArray(arr, i) || _nonIterableRest(); }

function _nonIterableRest() { throw new TypeError("Invalid attempt to destructure non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); }

function _unsupportedIterableToArray(o, minLen) { if (!o) return; if (typeof o === "string") return _arrayLikeToArray(o, minLen); var n = Object.prototype.toString.call(o).slice(8, -1); if (n === "Object" && o.constructor) n = o.constructor.name; if (n === "Map" || n === "Set") return Array.from(o); if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _arrayLikeToArray(o, minLen); }

function _arrayLikeToArray(arr, len) { if (len == null || len > arr.length) len = arr.length; for (var i = 0, arr2 = new Array(len); i < len; i++) arr2[i] = arr[i]; return arr2; }

function _iterableToArrayLimit(arr, i) { var _i = arr == null ? null : typeof Symbol !== "undefined" && arr[Symbol.iterator] || arr["@@iterator"]; if (_i == null) return; var _arr = []; var _n = true; var _d = false; var _s, _e; try { for (_i = _i.call(arr); !(_n = (_s = _i.next()).done); _n = true) { _arr.push(_s.value); if (i && _arr.length === i) break; } } catch (err) { _d = true; _e = err; } finally { try { if (!_n && _i["return"] != null) _i["return"](); } finally { if (_d) throw _e; } } return _arr; }

function _arrayWithHoles(arr) { if (Array.isArray(arr)) return arr; }

function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }

function _defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } }

function _createClass(Constructor, protoProps, staticProps) { if (protoProps) _defineProperties(Constructor.prototype, protoProps); if (staticProps) _defineProperties(Constructor, staticProps); return Constructor; }

function _inherits(subClass, superClass) { if (typeof superClass !== "function" && superClass !== null) { throw new TypeError("Super expression must either be null or a function"); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, writable: true, configurable: true } }); if (superClass) _setPrototypeOf(subClass, superClass); }

function _setPrototypeOf(o, p) { _setPrototypeOf = Object.setPrototypeOf || function _setPrototypeOf(o, p) { o.__proto__ = p; return o; }; return _setPrototypeOf(o, p); }

function _createSuper(Derived) { var hasNativeReflectConstruct = _isNativeReflectConstruct(); return function _createSuperInternal() { var Super = _getPrototypeOf(Derived), result; if (hasNativeReflectConstruct) { var NewTarget = _getPrototypeOf(this).constructor; result = Reflect.construct(Super, arguments, NewTarget); } else { result = Super.apply(this, arguments); } return _possibleConstructorReturn(this, result); }; }

function _possibleConstructorReturn(self, call) { if (call && (typeof call === "object" || typeof call === "function")) { return call; } else if (call !== void 0) { throw new TypeError("Derived constructors may only return object or undefined"); } return _assertThisInitialized(self); }

function _assertThisInitialized(self) { if (self === void 0) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return self; }

function _isNativeReflectConstruct() { if (typeof Reflect === "undefined" || !Reflect.construct) return false; if (Reflect.construct.sham) return false; if (typeof Proxy === "function") return true; try { Boolean.prototype.valueOf.call(Reflect.construct(Boolean, [], function () {})); return true; } catch (e) { return false; } }

function _getPrototypeOf(o) { _getPrototypeOf = Object.setPrototypeOf ? Object.getPrototypeOf : function _getPrototypeOf(o) { return o.__proto__ || Object.getPrototypeOf(o); }; return _getPrototypeOf(o); }

var GenerateMd = /*#__PURE__*/function (_GenerateClass) {
  _inherits(GenerateMd, _GenerateClass);

  var _super = _createSuper(GenerateMd);

  function GenerateMd(content, ops) {
    var _this;

    _classCallCheck(this, GenerateMd);

    _this = _super.call(this, content, ops);
    _this.content = _this.eliminateRedundancy();
    _this.ops = ops;
    return _this;
  }

  _createClass(GenerateMd, [{
    key: "run",
    value: function run() {
      var _this2 = this;

      var outputPath = this.ops.output;
      var mainPath = "".concat(outputPath, "/").concat(this.ops.main, ".md");
      var hasRelatedMap = new Map();
      var mainFileContent = '';
      this.content.forEach(function (content) {
        var hasRelated = _this2.relatedGrammar(content);

        if (hasRelated) {
          var _this2$relatedTitleAn = _this2.relatedTitleAndContent(content),
              _this2$relatedTitleAn2 = _slicedToArray(_this2$relatedTitleAn, 2),
              title = _this2$relatedTitleAn2[0],
              cont = _this2$relatedTitleAn2[1];

          _this2.relatedCollAdd(hasRelatedMap, title, cont);
        } else mainFileContent += _this2.wrap(_this2.generate(content));
      });
      hasRelatedMap.size && (0, _dir.createDir)((0, _posix.resolve)(outputPath));
      var hasRelatedMapKeys = hasRelatedMap.keys();

      var _loop = function _loop(i) {
        var relatedTitle = hasRelatedMapKeys.next().value;
        var item = hasRelatedMap.get(relatedTitle); // createDir(resolve(`${outputPath}/${relatedTitle}`))

        item.forEach(function (itemAST) {
          mainFileContent += _this2.generateHeadline(relatedTitle) + _this2.relatedLink(_this2.toLink(itemAST, "/".concat(outputPath, "/").concat(itemAST.name, ".md"))); // createFile(resolve(`${outputPath}/${relatedTitle}/${itemAST.name}.md`), this.generate(itemAST))
        });
      };

      for (var i = 0; i < hasRelatedMap.size; i++) {
        _loop(i);
      } // createFile(resolve(mainPath), mainFileContent)

    }
  }]);

  return GenerateMd;
}(_generateClass.GenerateClass);

exports.GenerateMd = GenerateMd;